<?php

if (!defined('BASEPATH'))
	exit('No direct script access allowed');

/**
 * Server_model Class
 *
 * @author Thy Ngoc
*/
class Ccu_model extends DataMapper {

	var $model = 'ccu_model';
	var $table = 'ccu';
	// --------------------------------------------------------------------
	var $has_one = array();
	var $has_many = array();
	// --------------------------------------------------------------------
	var $validation = array();
	// --------------------------------------------------------------------
	/**
	 * Default order in query select
	*/
	var $default_order_by = array('id');

	// --------------------------------------------------------------------
	/**
	 * string value of object
	 *
	 * @access private
	 * @param null
	 * @return string
	*/
	function __toString() {
		return empty($this->title) ? '---empty---' : $this->title;
	}

	function build_ccu(&$view, $date="", $time="", $limit=0, $render){
		//base variable
		$view           = new stdClass();

		$this->get_ccu_data($ccu_date_data, $date, "", 0, $render);
		$this->build_ccu_by_server($ccu_by_server, $ccu_date_data);

		//ccu chart
		$this->build_series_data($view->series_data, $ccu_by_server, $render, $date);

		$this->get_ccu_data($ccu_detail, $date, $time, $limit);

		$this->build_max_ccu($max_ccu, $date);
		$view->max_ccu_total = $view->whole_total = $view->ccu_total = 0;
		//server, name, online, whole, created, merge, partner, dns
		
		$view->ccu_detail = array();
		foreach($ccu_detail as $k=>$item){
			$view->ccu_detail[$k]['server']                 = $item->server;
			$view->ccu_detail[$k]['name']                   = $item->name;
			$view->ccu_detail[$k]['online']                 = $item->online;
			$view->ccu_detail[$k]['whole']                  = $item->whole;
			$view->ccu_detail[$k]['created']                = $item->created;
			$view->ccu_detail[$k]['merge']                  = $item->merge;
			$view->ccu_detail[$k]['partner']                = $item->partner;
			$view->ccu_detail[$k]['ip']                     = $item->ip;
			$view->ccu_detail[$k]['max_ccu']                = $max_ccu[$item->server]['max_ccu'];
			$view->ccu_detail[$k]['max_ccu_time']           = $max_ccu[$item->server]['max_ccu_time'];
			$view->ccu_total								+=$item->online;
			$view->max_ccu_total							+=$view->ccu_detail[$k]['max_ccu'];
			$view->whole_total								+=$item->whole;
		}
		ksort($view->ccu_detail);
		
		if($render!=""){
			$view->date_format = $this->extract_date_formart($render);
		}
		
		//exit($view->date_format);
		//print_r($view->ccu_detail);
		//exit;
	}
	
	/*
	 * hourly
	SELECT AVG(online) as online, HOUR(created) as hour, server, ip, partner, merge  FROM ccu 
	WHERE created like '2012-12-21%' group by HOUR(created) ,`server`
	ORDER BY created ASC
	
	* day
	SELECT AVG(online) as online, DAY(created) as hour, server, ip, partner, merge  FROM ccu 
	WHERE created like '2012-12%' group by DAY(created) ,`server`
	ORDER BY created ASC
	
	* month
	SELECT AVG(online) as online, MONTH(created) as hour, server, ip, partner, merge  FROM ccu 
	WHERE created like '2012%' group by MONTH(created) ,`server`
	ORDER BY created ASC 
	 */
	
	function get_ccu_data(&$ccu_data, $date="", $time="", $limit=0, $render=""){
		$ccu_data = array();
		//echo $time; exit;
		$datetime = $time!=""?$date." ".$time:$date;
		
		switch ($render) {
			case "hourly":	
				$this->select("server, name, AVG(online) as online, max(whole) as whole, HOUR(created) as created, merge, partner, ip");	
					
				$this->like('created', $date);
				
				$this->group_by('HOUR(created)')
					->group_by('server');
			break;
			
			case "daily":	
				$this->select("server, name, AVG(online) as online, max(whole) as whole, DAY(created) as created, merge, partner, ip");	
				
				$this->like('created', substr($date, 0, 7));
				
				$this->group_by('DAY(created)')
					->group_by('server');
			break;
			
			case "monthly":
				$this->select("server, name, AVG(online) as online, max(whole) as whole, MONTH(created) as created, merge, partner, ip");			

				$this->like('created', substr($date, 0, 4));
				
				$this->group_by('MONTH(created)')
					->group_by('server');
			break;
			
			default:
				$this->select("server, name, online, whole, created, merge, partner, ip");
				
				if($time==""){
					$this->like('created', $datetime);
					//$this->include_related('server', array("server, dns"));
				}else{
					$this->where('created <=', $datetime);
					//$this->include_related('server', array("server, dns"));
				}
			break;
			
		}
		
		if($limit){
			$this->limit($limit);
			$this->order_by("created", "desc");
		}else{
			$this->order_by("created", "asc");
		}
		$ccu_data = $this->get();
		//$this->check_last_query();
		//exit;
	}

	function build_ccu_by_server(&$ccu_by_server, $ccu_date_data){
		$ccu_by_server = array();
		foreach ($ccu_date_data as $k=>$item){
			$server_name = $item->server.' '.$item->name;
			$ccu_by_server[$server_name]['online'][]   = $item->online;
			$ccu_by_server[$server_name]['whole'][]    = $item->whole;
			$ccu_by_server[$server_name]['created'][]  = $item->created;
		}
	}

	function build_series_data(&$series_data, $ccu_by_server, $render, $date){
		$series_data = array();
		//build series_data
		$series_data="[";
		foreach ($ccu_by_server as $k => $item) {
			
			$series_data.="{name: '{$k}', data: [";
			foreach($item['online'] as $k1=>$item1){
				//echo $item['created'][$k1]
				//echo $k1;exit;
				// $series_data.="[Date.UTC(".substr(str_replace(' ', ',',str_replace(':', ',', str_replace('-', ',', $item['created'][$k1]))),0, 16)."), {$item1}, {$item['whole'][$k1]}],";
				$series_data.="[Date.UTC(".substr(str_replace(' ', ',',str_replace(':', ',', str_replace('-', ',', $this->extract_created($item['created'][$k1], $render, $date)))),0, 16)."), {$item1}],";
			}
			$series_data.="]},";
		}
		$series_data.="]";
	}

	function extract_created($created, $render, $date){
		switch ($render) {
			case "hourly":
				if(strlen($created)==1){
					$created = $date." 0".$created."-00";
				}					
				else{
					$created = $date." ".$created."-00";
				}
				return $created;
				//exit;
			break;
			
			case "daily":
				if(strlen($created)==1){
					$created = substr($date, 0, 7)." 0".$created." 00-00";
				}					
				else{
					$created = substr($date, 0, 7)."-".$created." 00-00";
				}
				return $created;
				//exit;
			break;
			
			case "monthly":
				if(strlen($created)==1){
					$created = substr($date, 0, 4)."-".$created."-00 00-00";
				}
				else{
					$created = substr($date, 0, 4)."-".$created."-00 00-00";
				}
				return $created;
				//exit;
			break;
			
			default:
				return $created;
			break;
		}
	}
	
	function extract_date_formart($render){		
		switch ($render) {							
			case "daily":
				return '%Y-%m-%d';
			break;
					
			case "monthly":
				return '%Y-%m';
			break;
			
			case "hourly":
				return '%Y-%m-%d %H';
			break;
		}
	}
	
	function build_max_ccu(&$max_ccu, $date){
		$max_ccu_model = new Ccu_model();
		$max_ccu_model->select("max(online) as max_online, server");
		$max_ccu_model->group_by("server");
		$max_ccu_model->like("created", $date);
		$max_ccu_model->get(30);
		//$total_max = 0;
		$max_ccu = array();
		foreach($max_ccu_model as $item){
			$max_ccu[$item->server]['max_ccu']        = $item->max_online;
			$max_ccu[$item->server]['max_ccu_time']	  = $this->max_ccu_time($date, $item->max_online);
			//$max_ccu[$item->server]['max_ccu_time']   = $item->created;
			//$total_max								  +=  $item->max_online;
		}
		//return $max_ccu;
		// $this->check_last_query();
	}

	function max_ccu_time($date, $max_ccu){
		$max_ccu_time = new Ccu_model();
		$max_ccu_time->select("created");
		$max_ccu_time->like("created", $date);
		$max_ccu_time->where("online", $max_ccu);
		$max_ccu_time->get(1);
		return $max_ccu_time->created;
	}

	function total_server($date_pick, $time_pick){
		//print_r($date_pick);
		if($time_pick!=""){
			$last_created =  $date_pick." ".$time_pick;
		}else{
			$obj = new Ccu_model();
			$obj->select("created");
			$obj->order_by("created", "desc");
			$obj->like("created", $date_pick." ".$time_pick);
			$obj->get(1);
			$last_created = substr($obj->created, 0, 16);
		}
		
		$obj = new Ccu_model();
		$obj->select("server")
		->group_by("server");
		$obj->like("created", $last_created);
		$obj->get();
		
		//$obj->check_last_query();
		//exit;
		$total = 0;
		foreach($obj as $item){
			$total++;
		}
		return $total;
	}
	
	function extract_date_pick($render, $date){
		switch ($render) {
			case "hourly":
				return $date;
				//exit;
				break;
					
			case "daily":
				return substr($date, 0, 7);
				//exit;
				break;
					
			case "monthly":
				return substr($date, 0, 4);
				//exit;
				break;					
			default:
				return $date;
				break;
		}
	}

}

/* End of file server.php */
/* Location: ./application/models/server.php */